/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.curriculum.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.biz.modular.curriculum.param.*;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.biz.modular.curriculum.entity.EduCurriculum;
import vip.xiaonuo.biz.modular.curriculum.service.EduCurriculumService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;

/**
 * 教学计划表控制器
 *
 * @author ahui
 * @date  2023/05/28 22:59
 */
@Api(tags = "教学计划表控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class EduCurriculumController {

    @Resource
    private EduCurriculumService eduCurriculumService;

    /**
     * 获取教学计划表分页
     *
     * @author ahui
     * @date  2023/05/28 22:59
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取教学计划表分页")
    @SaCheckPermission("/biz/curriculum/page")
    @GetMapping("/biz/curriculum/page")
    public CommonResult<Page<EduCurriculum>> page(EduCurriculumPageParam eduCurriculumPageParam) {
        return CommonResult.data(eduCurriculumService.page(eduCurriculumPageParam));
    }

    /**
     * 添加教学计划表
     *
     * @author ahui
     * @date  2023/05/28 22:59
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加教学计划表")
    @CommonLog("添加教学计划表")
    @SaCheckPermission("/biz/curriculum/add")
    @PostMapping("/biz/curriculum/add")
    public CommonResult<String> add(@RequestBody @Valid EduCurriculumAddParam eduCurriculumAddParam) {
        eduCurriculumService.add(eduCurriculumAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑教学计划表
     *
     * @author ahui
     * @date  2023/05/28 22:59
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑教学计划表")
    @CommonLog("编辑教学计划表")
    @SaCheckPermission("/biz/curriculum/edit")
    @PostMapping("/biz/curriculum/edit")
    public CommonResult<String> edit(@RequestBody @Valid EduCurriculumEditParam eduCurriculumEditParam) {
        eduCurriculumService.edit(eduCurriculumEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除教学计划表
     *
     * @author ahui
     * @date  2023/05/28 22:59
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除教学计划表")
    @CommonLog("删除教学计划表")
    @SaCheckPermission("/biz/curriculum/delete")
    @PostMapping("/biz/curriculum/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<EduCurriculumIdParam> eduCurriculumIdParamList) {
        eduCurriculumService.delete(eduCurriculumIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取教学计划表详情
     *
     * @author ahui
     * @date  2023/05/28 22:59
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取教学计划表详情")
    @SaCheckPermission("/biz/curriculum/detail")
    @GetMapping("/biz/curriculum/detail")
    public CommonResult<EduCurriculum> detail(@Valid EduCurriculumIdParam eduCurriculumIdParam) {
        return CommonResult.data(eduCurriculumService.detail(eduCurriculumIdParam));
    }

    /**
     * 下载课程导入模板
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("下载教学计划导入模板")
    @CommonLog("下载教学计划导入模板")
    @GetMapping(value = "/biz/curriculum/downloadImportCurriculumTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportCourseTemplate(HttpServletResponse response) throws IOException {
        eduCurriculumService.downloadImportCurriculumTemplate(response);
    }

    /**
     * 课程导入
     *
     * @author xuyuxiang
     * @date 2023/5/27 20:00
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("教学计划导入")
    @CommonLog("教学计划导入")
    @PostMapping("/biz/curriculum/import")
    public CommonResult<JSONObject> importCurriculum(@RequestPart("file") @ApiParam(value = "文件", required = true) MultipartFile file,
                                                     @RequestPart("major") @ApiParam(value = "专业", required = true) String major,
                                                     @RequestPart("type") @ApiParam(value = "培养类型", required = true) String type,
                                                     @RequestPart("year") @ApiParam(value = "版本", required = true) String year) {
        return CommonResult.data(eduCurriculumService.importCurriculum(file, major, type, Integer.valueOf(year)));
    }

    /**
     * 教学计划导出
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("教学计划导出")
    @CommonLog("教学计划导出")
    @GetMapping(value = "/biz/curriculum/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportUser(EduCurriculumExportParam eduCurriculumExportParam, HttpServletResponse response) throws IOException {
        eduCurriculumService.exportCurriculum(eduCurriculumExportParam, response);
    }

    /**
     * 提取课程
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation("提取课程")
    @CommonLog("提取课程")
    @SaCheckPermission("/biz/curriculum/extract")
    @GetMapping("/biz/curriculum/extract")
    public CommonResult<String> extract(EduCurriculumExportParam eduCurriculumIdParams) {
        eduCurriculumService.extract(eduCurriculumIdParams);
        return CommonResult.ok();
    }

    /**
     * 分配课程（按专业、年级、学生类型）
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation("分配课程")
    @CommonLog("分配课程")
    @SaCheckPermission("/biz/curriculum/assign")
    @GetMapping("/biz/curriculum/assign")
    public CommonResult<String> assign(EduCurriculumAssignParam eduCurriculumAssignParam) {
        eduCurriculumService.assign(eduCurriculumAssignParam);
        return CommonResult.ok();
    }
}
